package com.prb.client.netty.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.prb.netty.AsyncResult;
import com.prb.client.netty.Client;
import com.prb.netty.protocol.SmartCarProtocol;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

import java.util.concurrent.locks.LockSupport;

//用于读取客户端发来的信息  
public class ClientHandler extends ChannelInboundHandlerAdapter {
  
    // 客户端与服务端，连接成功的售后
/*
   @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 发送SmartCar协议的消息
        // 要发送的信息
        String data = "I am client ...";
        // 获得要发送信息的字节数组
        byte[] content = data.getBytes();
        // 要发送信息的长度
        int contentLength = content.length;

        SmartCarProtocol protocol = new SmartCarProtocol(contentLength, content);

        ctx.writeAndFlush(protocol);
    }*/


    // 只是读数据，没有写数据的话

    // 需要自己手动的释放的消息  
    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg)  
            throws Exception {  
        try {  
            // 用于获取客户端发来的数据信息  
            SmartCarProtocol body = (SmartCarProtocol) msg;
            AsyncResult result = JSONObject.parseObject(new String(body.getContent()) , AsyncResult.class);

            String invokeId = result.getInvokeId();

            AsyncResult asyncResult = Client.asyncMap.get(invokeId);

            asyncResult.setResult(result.getResult());

            LockSupport.unpark(asyncResult.getThread());


        } finally {  
            ReferenceCountUtil.release(msg);  
        }  
    }  
  
    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)  
            throws Exception {  
        ctx.close();  
    }  
  
}  